package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.ExplainRelationListDTO;
import com.xbongbong.paas.pojo.dto.FormExplainListDTO;
import com.xbongbong.paas.pojo.dto.FormGetDTO;
import com.xbongbong.paas.pojo.dto.StageGetDTO;
import com.xbongbong.paas.pojo.dto.StageListDTO;
import com.xbongbong.paas.pojo.dto.StageSaveDTO;
import com.xbongbong.paas.pojo.dto.StageTitleFieldDTO;
import com.xbongbong.paas.pojo.dto.StageWorkGetDTO;
import com.xbongbong.paas.pojo.dto.StageWorkSaveDTO;
import com.xbongbong.paas.pojo.dto.StageWorkSortDTO;
import com.xbongbong.paas.pojo.dto.StageWorkflowListDTO;
import com.xbongbong.paas.pojo.dto.StartStageGetDTO;
import com.xbongbong.paas.pojo.vo.ExplainRelationListVO;
import com.xbongbong.paas.pojo.vo.LinkFormSimpleListVO;
import com.xbongbong.paas.pojo.vo.StageGetVO;
import com.xbongbong.paas.pojo.vo.StageLinkAttrVO;
import com.xbongbong.paas.pojo.vo.StageListVO;
import com.xbongbong.paas.pojo.vo.StageSaveVO;
import com.xbongbong.paas.pojo.vo.StageTitleFieldVO;
import com.xbongbong.paas.pojo.vo.StageWorkGetVO;
import com.xbongbong.paas.pojo.vo.StageWorkflowListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.stage.StageSettingService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.vo.RuleFormExplainListVO;
import com.xbongbong.pro.opportunity.pojo.dto.OpportunityStageSyncDTO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 阶段设置
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/11/26 18:19
 */
@SuppressWarnings("deprecation")
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/stage/setting")
public class StageSettingController {
    @Resource
    private StageSettingService stageSettingService;
    @Resource
    private PaasRedisHelper paasRedisHelper;


    /**
     * 阶段获取
     * @param stageGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid StageGetDTO stageGetDTO, BindingResult br) {
        XbbResponse<StageGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageGetVO stageGetVO = stageSettingService.get(stageGetDTO);
                response = new XbbResponse<>(stageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段保存
     * @param stageSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid StageSaveDTO stageSaveDTO, BindingResult br) {
        XbbResponse<StageSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(stageSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(stageSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                StageSaveVO stageSaveVO = stageSettingService.save(stageSaveDTO);
                response = new XbbResponse<>(stageSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段列表-包含任务列表
     * @param stageListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid StageListDTO stageListDTO, BindingResult br) {
        XbbResponse<StageListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageListVO stageListVO = stageSettingService.list(stageListDTO);
                response = new XbbResponse<>(stageListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段任务保存
     * @param stageWorkSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/work/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workSave(@RequestBody @Valid StageWorkSaveDTO stageWorkSaveDTO, BindingResult br) {
        XbbResponse<StageSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(stageWorkSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(stageWorkSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                StageSaveVO stageSaveVO = stageSettingService.stageWorkSave(stageWorkSaveDTO);
                response = new XbbResponse<>(stageSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取阶段任务
     * @param stageWorkGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/work/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workSave(@RequestBody @Valid StageWorkGetDTO stageWorkGetDTO, BindingResult br) {
        XbbResponse<StageWorkGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageWorkGetVO stageWorkGetVO = stageSettingService.stageWorkGet(stageWorkGetDTO);
                response = new XbbResponse<>(stageWorkGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段任务排序
     * @param stageWorkSortDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/work/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workSort(@RequestBody @Valid StageWorkSortDTO stageWorkSortDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = stageSettingService.stageWorkSort(stageWorkSortDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 开始阶段设置
     * @param stageSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/start/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String startSet(@RequestBody @Valid StageSaveDTO stageSaveDTO, BindingResult br) {
        XbbResponse<StageSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageSaveVO stageSaveVO = stageSettingService.startSet(stageSaveDTO);
                response = new XbbResponse<>(stageSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 开始阶段获取
     * @param startStageGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/start/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String startGet(@RequestBody @Valid StartStageGetDTO startStageGetDTO, BindingResult br) {
        XbbResponse<StageGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageGetVO stageGetVO = stageSettingService.startGet(startStageGetDTO);
                response = new XbbResponse<>(stageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段任务删除
     * @param stageWorkGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/work/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workDel(@RequestBody @Valid StageWorkGetDTO stageWorkGetDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = stageSettingService.stageWorkDel(stageWorkGetDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取消息内容字段
     * @param stageTitleFieldDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/explain/getTitleField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String titleField(@RequestBody @Valid StageTitleFieldDTO stageTitleFieldDTO, BindingResult br) {
        XbbResponse<StageTitleFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageTitleFieldVO stageTitleFieldVO = stageSettingService.getTitleField(stageTitleFieldDTO);
                response = new XbbResponse<>(stageTitleFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段比例或者任务占比变更同步销售机会数据
     * @param stageSyncDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/sync/opportunity", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String syncOpportunity(@RequestBody @Valid OpportunityStageSyncDTO stageSyncDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = stageSettingService.syncOpportunityData(stageSyncDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 关联数据表单字段
     * @param stageTitleFieldDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/explain/linkAttr/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String linkAttr(@RequestBody @Valid StageTitleFieldDTO stageTitleFieldDTO, BindingResult br) {
        XbbResponse<StageLinkAttrVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageLinkAttrVO stageLinkAttrVO = stageSettingService.getLinkAttr(stageTitleFieldDTO);
                response = new XbbResponse<>(stageLinkAttrVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取当前表单的关联表单信息
     * @param formGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/linkForm/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getLinkFormList(@RequestBody @Valid FormGetDTO formGetDTO, BindingResult br) {
        XbbResponse<LinkFormSimpleListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkFormSimpleListVO linkFormSimpleListVO = stageSettingService.getLinkFormList(formGetDTO);
                response = new XbbResponse<>(linkFormSimpleListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取条件及逻辑符号
     * @param formExplainListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "explain/ruleList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String ruleList(@RequestBody @Valid FormExplainListDTO formExplainListDTO, BindingResult br) {
        XbbResponse<RuleFormExplainListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RuleFormExplainListVO ruleFormExplainListVO = stageSettingService.ruleList(formExplainListDTO);
                response = new XbbResponse<>(ruleFormExplainListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 表单业务规则条件字段联动查询
     * @param explainCompareListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/relation/ruleList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String relationRuleList(@RequestBody @Valid ExplainRelationListDTO explainCompareListDTO, BindingResult br) {
        XbbResponse<ExplainRelationListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExplainRelationListVO explainRelationListVO = stageSettingService.relationRuleList(explainCompareListDTO);
                response = new XbbResponse<>(explainRelationListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取字段条件支持的字段
     * @param stageTitleFieldDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/explain/getfield", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String titleFieldConditions(@RequestBody @Valid StageTitleFieldDTO stageTitleFieldDTO, BindingResult br) {
        XbbResponse<StageTitleFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageTitleFieldVO stageTitleFieldVO = stageSettingService.getFieldConditions(stageTitleFieldDTO);
                response = new XbbResponse<>(stageTitleFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 阶段推进器设置获取可以选择的工作流
     * @param stageWorkflowListDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2022/2/9 16:41
     * @since
     */
    @RequestMapping(value = "/workflowList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowList(@RequestBody @Valid StageWorkflowListDTO stageWorkflowListDTO, BindingResult br) {
        XbbResponse<StageWorkflowListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageWorkflowListVO stageWorkflowListVO = stageSettingService.workflowList(stageWorkflowListDTO);
                response = new XbbResponse<>(stageWorkflowListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
